package cn.controller;

import cn.utils.ThreadLocalDateUtil;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.support.StandardMultipartHttpServletRequest;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.UUID;

/**
 * Created by Administrator on 2018/1/17 0017.
 */
@RestController
@RequestMapping("/activemq")
public class ImguploadController extends HttpServlet{
    @Value("${web.upload-path}")
    private String path;

    @RequestMapping(value = "/testUpload", method = RequestMethod.POST)
    public void testUploadFile(HttpServletRequest req, MultipartHttpServletRequest multiReq) throws IOException {
        //设置编码格式
        //req.setCharacterEncoding("UTF-8");
        //是否文件上传组件
        boolean multipartContent = ServletFileUpload.isMultipartContent(req);
        System.out.println(multipartContent);
        //使用包去解析内容--看方法-->使用文档
        // 创建磁盘文件工厂
        DiskFileItemFactory factory = new DiskFileItemFactory();
        // 创建文件上传处理器
        ServletFileUpload upload = new ServletFileUpload(factory);
        //单个文件大小
        //	upload.setFileSizeMax(1024*10);//10KB
        //设置总文件大小
        //	upload.setSizeMax(1024*20);//30KB
        StandardMultipartHttpServletRequest request = (StandardMultipartHttpServletRequest) req;
        System.out.println(req.getParameter("name"));
        System.out.println(req.getParameter("pwd"));
        System.out.println(request.getParameter("imgHead"));
        Iterator<String> iterator = request.getFileNames();
        while (iterator.hasNext()) {
            MultipartFile file = request.getFile(iterator.next());
            String fileNames = file.getOriginalFilename();
            String fileName = UUID.randomUUID().toString() + "." + FilenameUtils.getExtension(fileNames);
            System.out.println(fileNames);
            System.out.println(fileName);
            File filePath ;
            try {
                String paths = path + ThreadLocalDateUtil.formatDateNyr(new Date())+"/";
                filePath = new File(paths);
                System.out.println(paths);
                if (filePath.exists()){
                    FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
                    BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(paths + fileName));
                    byte[] bs = new byte[1024];
                    int len;
                    while ((len = fileInputStream.read(bs)) != -1) {
                        bos.write(bs, 0, len);
                    }
                    bos.flush();
                    bos.close();
                }else {
                    try {
                        //mkdirs()可以建立多级文件夹， mkdir()只会建立一级的文件夹，
                        filePath.mkdir();
                        //filePath.mkdirs();
                        FileInputStream fileInputStream = (FileInputStream) file.getInputStream();
                        BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(paths + fileName));
                        byte[] bs = new byte[1024];
                        int len;
                        while ((len = fileInputStream.read(bs)) != -1) {
                            bos.write(bs, 0, len);
                        }
                        bos.flush();
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

